package com.niit.dao.impl;

import java.util.List;

import com.niit.bean.Question;
import com.niit.dao.QuestionDao;
import com.niit.utils.DbUtil;

public class QuestionDaoImpl implements QuestionDao {

	private DbUtil util =new DbUtil();
	@Override
	public int add(Question t) {
		String sql="insert into tb_question(author,classify,title,content) values(?,?,?,?)";
		int a=util.update(sql,t.getAuthor(), t.getClassify(),t.getTitle(),t.getContent());
		return a;
	}

	@Override
	public int delete(Question t) {
		String sql="delete from tb_question where quesId=? ";
		int a=util.update(sql, t.getQuesId());
		return a;
	}

	@Override
	public int update(Question t) {
		String sql="update tb_question set checked=? where quesId=?";
		int a =util.update(sql, t.isChecked(),t.getQuesId());
		return a;
	}


	@Override
	public List<Question> findAll(int start, int end) {
		String sql="select * from (select rownum r,tb_question.* from tb_question) a where a.r>? and a.r<=?";
		List<Question> list =util.select(sql, Question.class, start,end);
		return list;
	}

	@Override
	public int findTotal() {
		String sql = "select count(*) from tb_question ";
		int count = util.selectCount(sql);
		return count;
	}
	
	@Override
	public Question findByQuesId(String quesId) {
		String sql = "select * from tb_question where quesId=?";
		List<Question> list = util.select(sql, Question.class, quesId);
		int count = list.size();
		if(count==0){
			return null;
		}else{
			Question question = list.get(0);
			return question;
		}
	}

	@Override
	public List<Object[]> findQuesAndCount4Reply() {
		String sql = "select tb_question.*,(select count(1) from tb_ques_reply where tb_question.quesid = tb_ques_reply.quesid) from tb_question";
		return util.selectArray(sql);
	}

	@Override
	public int findTotalByClassify(String classify) {
		String sql = "select count(*) from tb_question where classify = ?";
		return util.selectCount(sql, classify);
	}

	@Override
	public List<Question> getPageByClassify(String classify, int start, int end) {
		String sql = null;
		List<Question> list = null;
		if(classify!=null){
			sql = "select * from (select rownum r,tb_question.* from tb_question where classify=?) a where a.r>? and a.r<=?";
			list =util.select(sql, Question.class,classify, start,end);
		}else{
			sql = "select * from (select rownum r,tb_question.* from tb_question) a where a.r>? and a.r<=?";
			list =util.select(sql, Question.class, start,end);
		}
		return list;
	}

	@Override
	public String findMaxQuesId() {
		String sql ="select max(quesId) from tb_question";
		return util.selectCount(sql)+"";
	}

}
